package com.dzsw.javabase.regx.sqllike;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LikeExtractor {

    public static final String REGEX = "(?i)(`[^`]+`|[\\w.]+)\\s+LIKE\\s+(\\?|'[^']*'|\"[^\"]*\"|#\\{[^}]*\\})(?:\\s+ESCAPE\\s+['\"]([^'\"]*)['\"])?";

    public static List<LikeCondition> extractLikeConditions(String sql) {
        List<LikeCondition> conditionList = new ArrayList<>();
        Pattern pattern = Pattern.compile(REGEX);
        Matcher matcher = pattern.matcher(sql);
        while (matcher.find()) {
            conditionList.add(new LikeCondition(
                    matcher.group(0),
                    matcher.group(1),
                    matcher.group(2),
                    matcher.group(3)
            ));
        }
        return conditionList;
    }

}
